﻿<html>
<body>
  <p>
    Class <strong>
      JPushClient
    </strong>
    <br />
    This class is the core for calling JPush API via official RESTful interfaces.
    There are two major features:
    <ul>
      <li>Send Push message for iOS and/or Android and/or Windows Phone</li>
      <li>Track delivery status via multiple message ID, which returned by JPush Service.</li>
    </ul>
   </p>
  <p>&nbsp;</p>
  <p>
    <strong>Sample:</strong>
  </p>
  <p>Here is a sample based on .NET console application.</p>
  <div class="code">
    <table width="100%" cellspacing="0" cellpadding="0">
      <tr>
      <td>
          <pre xml:space="preserve"><span class="highlight-keyword" style="color: #0000FF;">using</span> System;
<span class="highlight-keyword" style="color: #0000FF;">using</span> System.Collections.Generic;
<span class="highlight-keyword" style="color: #0000FF;">using</span> System.IO;
<span class="highlight-keyword" style="color: #0000FF;">using</span> System.Net;
<span class="highlight-keyword" style="color: #0000FF;">using</span> System.Text;
<span class="highlight-keyword" style="color: #0000FF;">using</span> System.Threading;
<span class="highlight-keyword" style="color: #0000FF;">using</span> ifunction.JPush.V3;
<span class="highlight-keyword" style="color: #0000FF;">using</span> Newtonsoft.Json;
<span class="highlight-keyword" style="color: #0000FF;">namespace</span> ifunction.JPush.Test
{
    <span class="highlight-keyword" style="color: #0000FF;">class</span> Program
    {
        <span class="highlight-keyword" style="color: #0000FF;">static</span> <span class="highlight-keyword" style="color: #0000FF;">void</span> Main(<span class="highlight-keyword" style="color: #0000FF;">string</span>[] args)
        {
            <span class="highlight-keyword" style="color: #0000FF;">var</span> appKey = <span class="highlight-literal" style="color: #CC0000;">"1234567890abcdef"</span>; <span class="highlight-comment" style="color: #006633;">// Your App Key from JPush</span>
            <span class="highlight-keyword" style="color: #0000FF;">var</span> masterSecret = <span class="highlight-literal" style="color: #CC0000;">"1234567890abcdef"</span>; <span class="highlight-comment" style="color: #006633;">// Your Master Secret from JPush</span>

            Dictionary&lt;<span class="highlight-keyword" style="color: #0000FF;">string</span>, <span class="highlight-keyword" style="color: #0000FF;">string</span>&gt; customizedValues = <span class="highlight-keyword" style="color: #0000FF;">new</span> Dictionary&lt;<span class="highlight-keyword" style="color: #0000FF;">string</span>, <span class="highlight-keyword" style="color: #0000FF;">string</span>&gt;();
            customizedValues.Add(<span class="highlight-literal" style="color: #CC0000;">"CK1"</span>, <span class="highlight-literal" style="color: #CC0000;">"CV1"</span>);
            customizedValues.Add(<span class="highlight-literal" style="color: #CC0000;">"CK2"</span>, <span class="highlight-literal" style="color: #CC0000;">"CV2"</span>);

            JPushClientV3 client = <span class="highlight-keyword" style="color: #0000FF;">new</span> JPushClientV3(appKey, masterSecret);

            Audience audience = <span class="highlight-keyword" style="color: #0000FF;">new</span> Audience();
            <span class="highlight-comment" style="color: #006633;">// In JPush V3, tag can be multiple added with different values.</span>
            <span class="highlight-comment" style="color: #006633;">// In following code, it is to send push to those who are in ((Tag1 AND Tag2) AND (Tag3 OR Tag4))</span>
            <span class="highlight-comment" style="color: #006633;">// If you want to send to all, please use: audience.Add(PushTypeV3.Broadcast, null);</span>
            audience.Add(PushTypeV3.ByTagWithinAnd, <span class="highlight-keyword" style="color: #0000FF;">new</span> List&lt;<span class="highlight-keyword" style="color: #0000FF;">string</span>&gt;(<span class="highlight-keyword" style="color: #0000FF;">new</span> <span class="highlight-keyword" style="color: #0000FF;">string</span>[] { <span class="highlight-literal" style="color: #CC0000;">"Tag1"</span>, <span class="highlight-literal" style="color: #CC0000;">"Tag2"</span> }));
            audience.Add(PushTypeV3.ByTagWithinOr, <span class="highlight-keyword" style="color: #0000FF;">new</span> List&lt;<span class="highlight-keyword" style="color: #0000FF;">string</span>&gt;(<span class="highlight-keyword" style="color: #0000FF;">new</span> <span class="highlight-keyword" style="color: #0000FF;">string</span>[] { <span class="highlight-literal" style="color: #CC0000;">"Tag3"</span>, <span class="highlight-literal" style="color: #CC0000;">"Tag4"</span> }));

            <span class="highlight-comment" style="color: #006633;">// In JPush V3, Notification would not be display on screen, it would be transferred to app instead.</span>
            <span class="highlight-comment" style="color: #006633;">// And different platform can provide different notification data.</span>
            Notification notification = <span class="highlight-keyword" style="color: #0000FF;">new</span> Notification
            {
                AndroidNotification = <span class="highlight-keyword" style="color: #0000FF;">new</span> AndroidNotificationParameters
                {
                    Title = <span class="highlight-literal" style="color: #CC0000;">"JPush provides V3."</span>,
                    Alert = <span class="highlight-literal" style="color: #CC0000;">"JPush V2 would be retired soon."</span>,
                    CustomizedValues = customizedValues
                },
                iOSNotification = <span class="highlight-keyword" style="color: #0000FF;">new</span> iOSNotificationParameters
                {
                    Badge = <span class="highlight-number">1</span>,
                    Alert = <span class="highlight-literal" style="color: #CC0000;">"JPush V2 would be retired soon."</span>,
                    Sound = <span class="highlight-literal" style="color: #CC0000;">"YourSound"</span>,
                    CustomizedValues = customizedValues
                }
            };

            <span class="highlight-keyword" style="color: #0000FF;">var</span> response = client.SendPushMessage(<span class="highlight-keyword" style="color: #0000FF;">new</span> PushMessageRequestV3
            {
                Audience = audience,
                Platform = PushPlatform.AndroidAndiOS,
                IsTestEnvironment = <span class="highlight-keyword" style="color: #0000FF;">true</span>,
                AppMessage = <span class="highlight-keyword" style="color: #0000FF;">new</span> AppMessage
                {
                    Content = <span class="highlight-literal" style="color: #CC0000;">"Hello, this is a test push of V3 from .NET. Have a nice day!"</span>,
                    CustomizedValue = customizedValues
                },
                Notification = notification
            });

            Console.WriteLine(response.ResponseCode.ToString() + <span class="highlight-literal" style="color: #CC0000;">":"</span> + response.ResponseMessage);
            Console.WriteLine(<span class="highlight-literal" style="color: #CC0000;">"Push sent."</span>);
            Console.WriteLine(response.ResponseCode.ToString() + <span class="highlight-literal" style="color: #CC0000;">":"</span> + response.ResponseMessage);


            List&lt;<span class="highlight-keyword" style="color: #0000FF;">string</span>&gt; idToCheck = <span class="highlight-keyword" style="color: #0000FF;">new</span> List&lt;<span class="highlight-keyword" style="color: #0000FF;">string</span>&gt;();
            idToCheck.Add(response.MessageId);
            <span class="highlight-keyword" style="color: #0000FF;">var</span> statusList = client.QueryPushMessageStatus(idToCheck);
            Console.WriteLine(<span class="highlight-literal" style="color: #CC0000;">"Status track is completed."</span>);
            <span class="highlight-keyword" style="color: #0000FF;">if</span> (statusList != <span class="highlight-keyword" style="color: #0000FF;">null</span>)
            {
                <span class="highlight-keyword" style="color: #0000FF;">foreach</span> (<span class="highlight-keyword" style="color: #0000FF;">var</span> one <span class="highlight-keyword" style="color: #0000FF;">in</span> statusList)
                {
                    Console.WriteLine(<span class="highlight-keyword" style="color: #0000FF;">string</span>.Format(<span class="highlight-literal" style="color: #CC0000;">"Id: {0}, Android: {1}, iOS: {2}"</span>, one.MessageId, one.AndroidDeliveredCount, one.ApplePushNotificationDeliveredCount));
                }
            }
            Console.WriteLine(<span class="highlight-literal" style="color: #CC0000;">"Press any key to exit."</span>);
            Console.Read();
        }
    }
}</pre></td></tr>
    </table>
  </div>
    <p>
        If you already use JPushClient V2 (Class: JPushClient), you can continue to use that. Library would convert the old PushMessageRequest to PushMessageRequestV3. 
        <strong>NOTE: the parameter in consturctor of JPushClient has lost effect. Whatever you set as true or false, SSL would be used according to JPush's new rule.</strong>
    RESTful API reference: <a href="http://docs.jpush.cn/display/dev/Push-API-v3">http://docs.jpush.cn/display/dev/Push-API-v3</a>
  </p>
  <p>
    <strong>NOTE:</strong>
    <ul>
      <li>As official document defined, the message ID collection is limited by 1000 in one track request. So if more than 1000 is set in collection for tracking, only the first 1000 can get status information in result.</li>
      <li>Regarding to the possible change of official RESTful API, current client is cliamed to base on v3 version of JPush.</li>
    </ul>
  </p>

</body>
</html>